package net.itgoo.parkingpay.ui.payconfirm;

import net.itgoo.parkingpay.manage.ParkingManage;
import net.itgoo.parkingpay.rest.model.ParkingOrderModel;
import net.itgoo.parkingpay.rest.model.ParkingPrePayModel;
import net.itgoo.parkingpay.vendor.mvp.ParkingMVPLoadDataCallback;

import java.util.Map;

/**
 * Created by apple on 16/11/21.
 */
public class ParkingPayConfirmPresenter implements ParkingPayConfirmContract.Presenter {

    private final ParkingPayConfirmContract.View mView;
    private final ParkingPayConfirmContract.DataSource mRepository;
    private Map<String, String> mParams;

    public ParkingPayConfirmPresenter(Map<String, String> params, ParkingPayConfirmContract.View view,
                                      ParkingPayConfirmContract.DataSource repository) {
        mView = view;
        mRepository = repository;
        mParams = params;
        view.setPresenter(this);
    }

    @Override
    public void start() {

    }

    @Override
    public void stop() {
        mRepository.cancel();
    }

    @Override
    public void createOrder() {
        String userId = ParkingManage.getInstance(mView.getContext()).getUserId();
        long id = Long.valueOf(mParams.get("id"));
        mRepository.createOrder(userId, id, new ParkingMVPLoadDataCallback<ParkingOrderModel>() {
            @Override
            public void onDataLoaded(ParkingOrderModel data) {
                if (!mView.isActive()) {
                    return;
                }

                mView.showData(data);
            }

            @Override
            public void onDataError(String error) {
                if (!mView.isActive()) {
                    return;
                }

                mView.showDataError(error);
            }
        });

        mView.showLoading();
    }

    @Override
    public void prePay() {
        ParkingOrderModel order = mRepository.order();
        if (order != null) {
//            String sessionId = ParkingManage.getInstance(mView.getContext()).getSessionId();
//            mRepository.prePay(order.orderNo, sessionId, new ParkingMVPLoadDataCallback<ParkingPrePayModel>() {
//                @Override
//                public void onDataLoaded(ParkingPrePayModel data) {
//                    if (!mView.isActive()) {
//                        return;
//                    }
//                }
//
//                @Override
//                public void onDataError(String error) {
//                    if (!mView.isActive()) {
//                        return;
//                    }
//
//                    mView.showPrePayFailure(error);
//                }
//            });
        }

        mView.showHUD();
    }
}
